const arr2tree = list => {
  const res = [];
  const map = list.reduce((curRes, v) => ((curRes[v.id] = v), curRes), {});
  for (const item of list) {
    if (item.pid === 0) {
      res.push(item);
      continue;
    }
    if (item.pid in map) {
      const parent = map[item.pid];
      parent.children = parent.children || [];
      parent.children.push(item);
    }
  }
  return res;
};

module.exports = {
  arr2tree,
};
